$(function () {
    BASE.COMMON.getCommentReply(1);
    BASE.SCOURCECONTENT.initMember();
    BASE.SCOURCECONTENT.scanceProject();
    BASE.COMMON.getStatus(BASE.COMMON.getId(),8);
});
var BASE = BASE || {};
BASE.SCOURCECONTENT =(function () {
    /*浏览*/
    function scanceProject() {
        BASE.apiPost('/device/sourceCode/click',{ id: BASE.COMMON.getId() })
    }
    /*点赞*/
    function zanSource(obj) {
        BASE.apiPost('/device/sourceCode/like',{ id: BASE.COMMON.getId() },function () {
            $(obj).hide().next().fadeIn();
        })
    }
    /*取消点赞*/
    function unzanSource(obj) {
        BASE.apiPost('/device/sourceCode/unlike',{ id: BASE.COMMON.getId() },function () {
            $(obj).hide().prev().fadeIn();
        })
    }
    /*用户信息获取判断是否关注*/
    function initMember() {
        var userId = BASE.COMMON.getUserId();
        var curId = $('#content').data('memberid');
        if(userId){
            BASE.SCOURCECONTENT.getPan();
        }
        if (userId == curId){
            $('#memberModify').show();
            _getModifyData();
        }else {
            $('#memberNotice').show();
            BASE.COMMON.isNotice($('#memberNotice').data('memberid'),$('#memberNotice'));
        }
    }
    /*获取网盘信息*/
    function getPan() {
        BASE.getHttp('/device/sourceCode/getPan',{id:BASE.COMMON.getId()},(res)=>{
            if(res.code == 200 && res.data.panUrl){
                var tpl = template('downPanInfo_tpl',{panInfo: res.data});
                $('.downPanInfo').html(tpl);
            }else {
                $('.downPanInfo').html('');
            }
        })
    }
    /*所有分享送金币*/
    if(BASE.COMMON.getUserId()){
        $('body').on('click','.share-item',function () {
            BASE.postHttp('/device/sourceCode/share',{id:BASE.COMMON.getId()});
        });
        $('body').on('mouseenter','.icon-wechat',function () {
            BASE.postHttp('/device/sourceCode/share',{id:BASE.COMMON.getId()});
        });
    }
    /*修改部分start******************************/
    /*修改展示*/
    function modifyPopupShow() {
        if($('#memberModify button').data('get_pan')){
            _initPopUp();
            return;
        }
       _getModifyData(_initPopUp);
    }
    function _initPopUp() {
        var obj = $('#memberModify button');
        var title = $(obj).data('title');
        var price = $(obj).data('price');
        var covers = $(obj).data('covers');
        var supportPays = $(obj).data('support_pays');
        var content = $(obj).data('content');
        var categoryid = $(obj).data('categoryid');
        var panUrl = $(obj).data('pan_url');
        var panPwd = $(obj).data('pan_pwd');
        var replyShowIs = $(obj).data('reply_show_is');
        $('#title').val(title);
        BASE.UPLOADANSWER.ue.txt.html(content);
        _initPayType(supportPays);
        _initPrice(price);
        _initCoversModify(covers);
        _initCatagoryModify(categoryid);
        if($(obj).data('file_url')){
            $('#fileUrlLink').show().find('span').text('项目需求文档');
        }
        if(replyShowIs){
            $('#down_type').trigger('click');
        }
        $('#fileLink').val(panUrl);
        $('#filePas').val(panPwd);
        $('#answer_pop').show();
    }
    /*初始化付款类型*/
    function _initPayType(suport) {
        if(suport == 'rmb'){
            $('input[name="pay-type"]:eq(0)').trigger('click');
            return;
        }
        $('input[name="pay-type"]:eq(1)').trigger('click');
    }
    /*初始化价格*/
    function _initPrice(price) {
       if(price>0){
           $('#price').val(price);
           return;
       }
        $('#price_type2').trigger('click');
    }
    /*修改前获取链接等等*/
    function _getModifyData(callback) {
        var sendData = {
            id:BASE.COMMON.getId(),
            needStatus:false,
        };
        BASE.getHttp('/device/sourceCode/detail',sendData,function (res) {
            var data = res.data;
            if(res.code == 200 && (data.panUrl || data.attachUrl)){
                var $mod = $('#memberModify button');
                $mod.data('pan_url',data.panUrl);
                $mod.data('pan_pwd',data.panPwd);
                $mod.data('get_pan',true);
                $('body').data('file-url',data.attachUrl);
                if(callback && typeof callback == 'function'){
                    callback();
                }
            }
        });
    }
    /*初始化封面内容*/
    function _initCoversModify(covers) {
        BASE.POPUP.SELECTEDCOVERS = [];
        if(covers){
            var imgs = covers.split(' ');
            imgs.forEach(function (v) {
                BASE.POPUP.SELECTEDCOVERS.push({
                    url:v,
                    id:'',
                });
            });
            $('#selectedCovers').html(template('selectedCovers_tpl',{list:BASE.POPUP.SELECTEDCOVERS}));
        }
    }
    /*初始化显示分类*/
    function _initCatagoryModify(categoryid) {
        BASE.POPUP.getParentIdByid(categoryid,function (parentId) {
            if(parentId){
                BASE.POPUP.selectInterestPrarent(parentId,function () {
                    $('#interestChildData').val(categoryid);
                    $('#child_'+categoryid).addClass('hover').siblings().removeClass('hover');
                });
            }else {
                BASE.POPUP.selectInterestPrarent(categoryid,function () {
                    $('#interestData').val(categoryid);
                    $('#interestChildData').val('');
                    $('#parent_'+categoryid).addClass('hover').siblings().removeClass('hover');
                });
            }
        });
    }
    /*确定修改*/
    function sureModify(obj) {
        var sendData = _getAddData();
        if(!sendData){
            return;
        }
        $(obj).attr('disabled',true);
        BASE.postHttp('/device/sourceCode/update',sendData,function (res) {
            if(res.code != 200){
                BASE.clearpop(res.message || '修改失败');
                $(obj).attr('disabled',false);
                return;
            }
            $('#answer_pop').hide();
            window.location.reload();
        })
    }
    /*获取修改的数据*/
    function _getAddData() {
        var sendData = {
            title: $('#title').val(),
            content: BASE.UPLOADANSWER.ue.txt.html(),
            covers: BASE.POPUP.getCovers().join(' '),
            categoryId: $('#interestChildData option:selected').val() ||  $('#interestData option:selected').val(),
            attachUrl:$('body').data('file-url'),
            panUrl:$('#fileLink').val(),
            panPwd:$('#filePas').val(),
            price:$('#price').val(),
            supportPays:$('input[name="pay-type"]:checked').val(), //人民币金币*/
            replyShowIs:$('#down_type').is(':checked'),
            id:BASE.COMMON.getId(),
        };
        var payType = $('input[name="price-type"]:checked').val(); //免费收费
        if(payType == 2){ //免费
            sendData.price = 0;
        }
        if(payType == 1){ //收费
            delete sendData.replyShowIs;
        }
        if(!sendData.title){
            BASE.clearpop('请输入项目标题');
            return false;
        }
        if(!sendData.content){
            BASE.clearpop('请输入项目简介');
            return false;
        }
        if(payType == 1){
            if(!sendData.price){
                BASE.clearpop('请输入价格');
                return false;
            }
            if(!sendData.supportPays){
                BASE.clearpop('请选择支付方式');
                return false;
            }
        }
        if(!sendData.panUrl && !sendData.attachUrl ){
            BASE.clearpop('请上传附件或填写网盘链接');
            return false;
        }
        return sendData;
    }
    /*修改部分end******************************/
    /*源码的下载购买部分start******************************/
   /* 购买*/
    function buySource(obj) {
        $(obj).attr('disabled',true);
        var sendData = {
            sourceCodeId:BASE.COMMON.getId(),
        };
        BASE.postHttp('/device/order/sourceCode/buy',sendData,function (res) {
            if(res.code!=200){
                BASE.clearpop(res.message);
                $(obj).attr('disabled',false);
                return;
            }
            location.href = '/userSubmitOrder.html?id='+res.data.id;
        })
    }
   
    /*源码的下载购买部分end******************************/
    return {
        initMember:initMember,
        scanceProject:scanceProject,
        unzanSource:unzanSource,
        zanSource:zanSource,
        getPan:getPan,

        modifyPopupShow:modifyPopupShow,
        sureModify:sureModify,

        buySource:buySource,
    }
})();

